#ifndef __THREAD_POOL__
#define __THREAD_POOL__
#include "threadsafe_queue.h"
#include "join_threads.h"
#include <atomic>
#include <thread>
#include <vector>
#include <functional>

using std::thread;
using std::vector;
using std::atomic_bool;
using std::function;

class thread_pool
{
    using task_type = function<void()>;
private:
    atomic_bool _done;
    threadsafe_queue<task_type> _work_queue; // 工作队列
    size_t _thread_num;
    vector<thread>  _threads;
    join_threads _joiner; // 自动汇合全部线程:成员中第一个被销毁

    void worker_thread(); // 线程取任务执行
public:
    thread_pool(size_t thread_num);
    ~thread_pool();
    template <typename FunctionType>
    void submit(FunctionType f)
    {
        //将函数or可调用对象包装为std::function实例 再压入队列
        _work_queue.push(function<void()>(f)); 
    }
};
#endif